var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var point_map = require("./pointMap");

var task_ad = {
  is_over_today: false,
  go_page_from_home() {
    var logger = utils.create_logger("task_ad go_page_from_home: ");
    logger("");
    if (this.is_over_today) {
      logger("今天的广告任务已全部执行完毕");
      return;
    }
    // utils.click_point.apply(
    //   utils,
    //   utils.get_size_point(point_map["首页赚钱"], 1080, 2340)
    // );
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    this.init();
  },
  init() {
    var logger = utils.create_logger("task_ad init: ");
    logger("");
    var ocr_text = ocr_utils.find_text("看广告赚金币", false);
    if (ocr_text) {
      logger("找到--看广告赚金币--");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(5, 0, 2, 1000));
      if (utils.exists_text_starts_with("反馈")) {
        logger("找到--反馈--");
        this.run();
      } else {
        logger("没找到--反馈--");
        this.after();
      }
    } else {
      logger("没找到--看广告赚金币--");
      this.after();
    }
  },
  run() {
    ad.init();
    this.after();
  },
  after() {
    console.log("task_ad after");
    if (utils.click_text_starts_with("开心收下")) {
      sleep(utils.get_random_interval(2, 0, 1, 1000));
    }
    this.next();
  },
  next() {
    console.log("task_ad next");
  },
};
module.exports = task_ad;
